home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / ctlib100.zip / INSTALL.LZH / BPTREES2.PAS < prev    next >
Pascal/Delphi Source File  |  1996-10-12  |  4KB  |  138 lines

  1. {**************************************************************************}
  2. {*  BitSoft Development, L.L.C.                                           *}
  3. {*  Copyright (C) 1995, 1996 BitSoft Development, L.L.C.                  *}
  4. {*  All rights reserved.                                                  *}
  5. {*  Containers Library demo                                               *}
  6. {**************************************************************************}
  7.  
  8. program BpTrees2;
  9.  
  10. {$X+}
  11.  
  12. { Sample program for opening a B+ tree. }
  13.  
  14. uses Objects, Containr, ctBpTree,
  15.      {$ifdef Windows}
  16.      WinCtr;
  17.      {$else}
  18.      Crt;
  19.      {$endif}
  20.  
  21. type
  22.   PContact = ^TContact;
  23.   TContact = record
  24.     FirstName : string[15];
  25.     LastName : string[20];
  26.     Phone : string[18];
  27.     Company : string [25];
  28.   end; { TContact }
  29.  
  30. type
  31.   PContactList = ^TContactList;
  32.   TContactList = object(TBPlusTree)
  33.     function KeyOf(Item : Pointer) : Pointer; virtual;
  34.   end; { TContactList }
  35.  
  36. function TContactList.KeyOf(Item : Pointer) : Pointer;
  37. begin
  38.   KeyOf := @PContact(Item)^.LastName;
  39. end;
  40.  
  41. procedure SetContactValues(ALastName, AFirstName, APhone,
  42.   ACompany : string; var ContactRec : TContact);
  43. begin
  44.   with ContactRec do
  45.   begin
  46.     FirstName := AFirstName;
  47.     LastName := ALastName;
  48.     Phone := APhone;
  49.     Company := ACompany;
  50.   end; { with }
  51. end;
  52.  
  53. procedure DisplayContacts(ContactList : PGraph);
  54.  
  55.   procedure PrintInfo (Item : Pointer); far;
  56.   begin
  57.     with PContact(Item)^ do
  58.       writeln(LastName, '':15 - Length(LastName),
  59.         FirstName, '':15 - Length(FirstName),
  60.         Phone, '':20 - Length(Phone),
  61.         Company, '':20 - Length(Company));
  62.   end;
  63.  
  64. begin
  65.   ContactList^.ForEach(@PrintInfo);
  66. end;
  67.  
  68. procedure DisplayFirst(ContactList : PGraph);
  69. var
  70.   Item : Pointer;
  71. begin
  72.   Item := ContactList^.First;
  73.   Writeln('First item:');
  74.   with PContact(Item)^ do
  75.     writeln(LastName, '':15 - Length(LastName),
  76.       FirstName, '':15 - Length(FirstName),
  77.       Phone, '':20 - Length(Phone),
  78.       Company, '':20 - Length(Company));
  79.   ContactList^.DoneItem(Item); { not required }
  80. end;
  81.  
  82. procedure DisplayLast(ContactList : PGraph);
  83. var
  84.   Item : Pointer;
  85. begin
  86.   Item := ContactList^.Last;
  87.   Writeln('Last item:');
  88.   with PContact(Item)^ do
  89.     writeln(LastName, '':15 - Length(LastName),
  90.       FirstName, '':15 - Length(FirstName),
  91.       Phone, '':20 - Length(Phone),
  92.       Company, '':20 - Length(Company));
  93.   ContactList^.DoneItem(Item); { not required }
  94. end;
  95.  
  96. procedure FindLastName(ContactList : PGraph; LastName : string);
  97. var
  98.   Item : Pointer;
  99. begin
  100.   Item := ContactList^.KeyFirst(@LastName);
  101.   Writeln('Item found with last name ''', LastName, ''':');
  102.   with PContact(Item)^ do
  103.     writeln(LastName, '':15 - Length(LastName),
  104.       FirstName, '':15 - Length(FirstName),
  105.       Phone, '':20 - Length(Phone),
  106.       Company, '':20 - Length(Company));
  107.   ContactList^.DoneItem(Item); { not required }
  108. end;
  109.  
  110. var
  111.   ContactList : PContactList;
  112.   Contact : TContact;
  113.   Stream : PBufStream;
  114.  
  115. begin
  116.   ClrScr;
  117.  
  118.   { Open the stream }
  119.   Stream := New(PBufStream, Init('btrees.dat', stOpen, 1024));
  120.  
  121.   { Open the B tree }
  122.   ContactList := New(PContactList, Open(Stream, 5, 2));
  123.  
  124.   DisplayContacts(ContactList);
  125.   Writeln;
  126.   DisplayFirst(ContactList);
  127.   Writeln;
  128.   DisplayLast(ContactList);
  129.   Writeln;
  130.   FindLastName(ContactList, 'Wagner');
  131.  
  132.   { Dispose of the B tree }
  133.   Dispose(ContactList, Done);
  134.  
  135.   { Dispose of the stream }
  136.   Dispose(Stream, Done);
  137. end.
  138.